//
//  MyMemberShipVC.m
//  HappyStore
//
//  Created by 李小辉 on 16/8/23.
//  Copyright © 2016年 LLJ. All rights reserved.
//

#import "MyMemberShipVC.h"

@interface MyMemberShipVC ()
@property (weak, nonatomic) IBOutlet UILabel *titleTimeStr;
@property (weak, nonatomic) IBOutlet UILabel *overTimeStr;

@end

@implementation MyMemberShipVC

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"白色背景"] forBarMetrics:UIBarMetricsDefault];
}
- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [self.navigationController.navigationBar setBackgroundImage:nil forBarMetrics:UIBarMetricsDefault];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    UILabel *labelTitle = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 190, 44)];
    labelTitle.font = [UIFont systemFontOfSize:19];
    labelTitle.backgroundColor = [UIColor clearColor];
    labelTitle.text = @"我的会员";
    labelTitle.textColor = RGB(45, 45, 45);
    labelTitle.textAlignment = NSTextAlignmentCenter;
    self.navigationItem.titleView = labelTitle;
    //设置返回按钮
    UIBarButtonItem *leftItem = [UIBarButtonItem itemWithTarget:self action:@selector(leftBarButtonItemClick) image:@"返回" highImage:@"返回"];
    self.navigationItem.leftBarButtonItem = leftItem;
    
    
    NSString *str = [self intervalSinceNow:self.levelovertime];
    if ([str floatValue] > 0) {
        self.titleTimeStr.text = str;
    }else{
        self.titleTimeStr.text = @"会员已到期";
    }

    NSDate *confromTimesp = [NSDate dateWithTimeIntervalSince1970: [self.levelovertime longLongValue]/1000];

    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"YYYY-MM-dd HH:mm:ss"];

    NSString *dateTime = [dateFormatter stringFromDate:confromTimesp];
    if ([dateTime isEqualToString:@"1970-01-01 08:00:00"]) {
        self.overTimeStr.text = @"无";
    }else{
        self.overTimeStr.text = [NSString stringWithFormat:@"%@",dateTime];
    }
    
    //开启侧滑
    self.navigationController.interactivePopGestureRecognizer.delegate = (id)self;
}


- (void)leftBarButtonItemClick{
      [self.navigationController popViewControllerAnimated:YES];
}

- (NSString *)intervalSinceNow: (NSString *) theDate
{

    
    NSDate* dat = [NSDate date];
    NSString *strtime = [NSString stringWithFormat:@"%ld", (long)[dat timeIntervalSince1970]];
    
    NSLog(@"%@",strtime);
    CGFloat time = [self.levelovertime longLongValue]/1000 - [strtime longLongValue];

    NSString *timeString=@"";
    

    
    if (time/3600<1) {
        timeString = [NSString stringWithFormat:@"%f", time/60];
        timeString = [timeString substringToIndex:timeString.length-7];
        timeString=[NSString stringWithFormat:@"剩余%@分", timeString];
        
    }
    if (time/3600>1&&time/86400<1) {
        timeString = [NSString stringWithFormat:@"%f", time/3600];
        timeString = [timeString substringToIndex:timeString.length-7];
        timeString=[NSString stringWithFormat:@"剩余%@小时", timeString];
    }
    if (time/86400>1)
    {
        timeString = [NSString stringWithFormat:@"%f", time/86400];
        timeString = [timeString substringToIndex:timeString.length-7];
        timeString=[NSString stringWithFormat:@"%@", timeString];
        
    }

    return timeString;
}

@end
